from django.contrib.auth.mixins import LoginRequiredMixin
from django.http import HttpResponse

from django.http.response import HttpResponse
from django.shortcuts import render
from django.http.request import HttpRequest
# Create your views here.

def set_cookie(request):
    username=request.GET.get("username")
    response = HttpResponse('set_cookie')

    response.set_cookie(key='username',value=username)

    return response

def get_cookie(request):
    cookies= request.COOKIES
    name=cookies.get('username')
    print(name)
    return HttpResponse('get_cookie')

def set_session(request):

    request.session['name'] = 'abc'

    return HttpResponse('set_session')

def get_session(request):

    #获取session
    # username=request.session['username']
    # username=request.session.get("username")
    
    request.session.flush()

    return HttpResponse('get_session')
####################类视图定义
def test(request):

    if request.method =='GET':
        return HttpResponse('get')

    else:
        return HttpResponse('post')

from django.views import View

class JDLoginView(View):
    #self 指的是实例对象 #request 指的是 请求对象
    def get(self,request):

        return HttpResponse('jd - login - get')

    def post(self,request):

        return HttpResponse('jd - login - post')

    def delete(self, request):
        return HttpResponse('delete')

class CenterView(View):

    def get(self,request):
        isLogin=False
        if isLogin:
            return HttpResponse('center_get')
        else:
            return  HttpResponse('请登录')

    def post(self,requesr):
        isLogin=False
        if isLogin:
            return HttpResponse('center_post')
        else:
            return HttpResponse('请登录')

class CenterView(View,LoginRequiredMixin):

    def get(self,request):


            return  HttpResponse('get')

    def post(self,request):

            return HttpResponse('post')